import supertest from 'supertest';
import http from 'http';

import Application from '../src/application';
import Context from '../src/context';
import { Filter } from '../src/utils/filter';

export default class TestServer {
  #app = new Application();
  #request?: supertest.SuperTest<supertest.Test>;

  use(filter: Filter<Context>): TestServer {
    this.#app.use(filter);
    return this;
  }

  request(): supertest.SuperTest<supertest.Test> {
    if (!this.#request) {
      this.#request = supertest(http.createServer(this.#app.callback()));
    }
    return this.#request;
  }
}
